package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.datamation.swadeshi.model.Reference;

public class ReferenceDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public ReferenceDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public String getNextNumVal(String cSettingsCode){
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}
		
		String nextNumVal = "";
		
		String query="SELECT "+dbHelper.FCOMPANYBRANCH_NNUM_VAL+
					" FROM "+dbHelper.TABLE_FCOMPANYBRANCH+
					" WHERE "+dbHelper.FCOMPANYBRANCH_BRANCH_CODE+" in (SELECT "+dbHelper.FSALREP_REP_CODE
																	+" FROM "+dbHelper.TABLE_FSALREP+")  AND "+dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE+"='"+cSettingsCode+"'";
		Cursor cursor = dB.rawQuery(query, null);
		int count =cursor.getCount();
		if(count>0){
			while (cursor.moveToNext()) {
			 nextNumVal = cursor.getString(0);
			}
		}else{
			nextNumVal="1";
		}
		
		return nextNumVal;
	}

	public ArrayList<Reference> getCurrentPreFix(String cSettingsCode){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<Reference> list = new ArrayList<Reference>();
		
		String selectRep = "select c.cCharVal,d.DealPreFix, s.repPrefix from fcompanySetting c, fsalRep s,fdealer d where c.cSettingscode='"+cSettingsCode+"' and d.DealCode in (select DealCode from fsalRep)" ;
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectRep, null);
		
		while(cursor.moveToNext()){
			
			Reference reference = new Reference();
			
			reference.setCharVal(cursor.getString(cursor.getColumnIndex(dbHelper.FCOMPANYSETTING_CHAR_VAL)));
			reference.setDealPreFix(cursor.getString(cursor.getColumnIndex(dbHelper.FDEALER_PREFIX)));
			reference.setRepPrefix(cursor.getString(cursor.getColumnIndex(dbHelper.FSALREP_REP_PREFIX)));
			
			list.add(reference);
			
		}
		
		return list;
	}
	
	
	public int getCount(String cSettingsCode){
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		int count=0;
		
		String query="SELECT "+dbHelper.FCOMPANYBRANCH_NNUM_VAL+
					" FROM "+dbHelper.TABLE_FCOMPANYBRANCH+
					" WHERE "+dbHelper.FCOMPANYBRANCH_BRANCH_CODE+" in (SELECT "+dbHelper.FSALREP_REP_CODE
																	+" FROM "+dbHelper.TABLE_FSALREP+") AND "+dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE+"='"+cSettingsCode+"'";
		Cursor cursor = dB.rawQuery(query, null);
		count =cursor.getCount();
		if(count>0){
			return count;
		}else{
			return count;
		}
		
		
	}
	

	public int InsetOrUpdate(String code, int nextNumVal) {
		int count =0;		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
			
	try{

		SalRepDS repDS=new SalRepDS(context);
			
		ContentValues values = new ContentValues();
		
		values.put(dbHelper.FCOMPANYBRANCH_NNUM_VAL,  nextNumVal+"");
			
		if(getCount(code)>0){
			count = (int) dB.update(dbHelper.TABLE_FCOMPANYBRANCH, values, dbHelper.FCOMPANYBRANCH_BRANCH_CODE+"='"+repDS.getCurrentRepCode()+"' AND "+dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE+"='"+code+"'", null);
		}else{
			
			values.put(dbHelper.FCOMPANYBRANCH_BRANCH_CODE,  repDS.getCurrentRepCode());
			values.put(dbHelper.FCOMPANYBRANCH_RECORD_ID,  "1");
			values.put(dbHelper.FCOMPANYBRANCH_CSETTINGS_CODE,  code);
			
			count = (int) dB.insert(dbHelper.TABLE_FCOMPANYBRANCH, null, values);
		}
		
		}finally {  
			dB.close();
		}
		return count;
				
	}
	
}
